package Exercise;

import java.util.concurrent.locks.ReentrantLock;

public class Account {
    public static void main(String[] args) {
        Bank bank = new Bank();
        Thread cus1 = new Thread(bank);
        Thread cus2 = new Thread(bank);
        cus1.setName("客户1");
        cus2.setName("客户2");

        cus1.start();
        cus2.start();
    }
}
class Bank implements Runnable{
    private int balance=1000;
    private int sumBalance=0;
    private int count  = 0;
    private ReentrantLock lock = new ReentrantLock();
    public Bank(){

    }
    public void setBalance(int balance){
        this.balance=balance;
    }
    public void  run(){
            while (true) {
                try {
                    lock.lock();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+":"+"您当前的余额为:"+sumBalance);
                    if (count<50) {
                        sumBalance += balance;
                        count++;
                        System.out.println(Thread.currentThread().getName()+"恭喜您存钱成功,存钱后的金额为:"+sumBalance);
                    }else break;
                }finally {
                    lock.unlock();
                }
            }
        }
    }

